package org.example.lethear.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.example.lethear.dto.comment.CommentCreateRequest;
import org.example.lethear.dto.comment.CommentDTO;
import org.example.lethear.dto.comment.CommentUpdateRequest;
import org.example.lethear.dto.common.PageResult;
import org.example.lethear.service.CommentService;
import org.example.lethear.utils.Result;
import org.springframework.web.bind.annotation.*;

/**
 * 评论管理控制器
 */
@Tag(name = "评论管理", description = "评论的增删改查、点赞、回复等相关接口")
@RestController
@RequestMapping("/comments")
@RequiredArgsConstructor
public class CommentController {
    
    private final CommentService commentService;
    
    /**
     * 创建评论
     */
    @Operation(summary = "创建评论", description = "发布新的评论或回复")
    @PostMapping
    public Result<CommentDTO> createComment(@Valid @RequestBody CommentCreateRequest request) {
        CommentDTO comment = commentService.createComment(request);
        return Result.success("评论创建成功", comment);
    }
    
    /**
     * 更新评论
     */
    @Operation(summary = "更新评论", description = "修改已发布的评论")
    @PutMapping("/{commentId}")
    public Result<CommentDTO> updateComment(
            @Parameter(description = "评论ID") @PathVariable Long commentId,
            @Valid @RequestBody CommentUpdateRequest request) {
        CommentDTO comment = commentService.updateComment(commentId, request);
        return Result.success("评论更新成功", comment);
    }
    
    /**
     * 删除评论
     */
    @Operation(summary = "删除评论", description = "删除指定的评论")
    @DeleteMapping("/{commentId}")
    public Result<Void> deleteComment(@Parameter(description = "评论ID") @PathVariable Long commentId) {
        commentService.deleteComment(commentId);
        return Result.success("评论删除成功");
    }
    
    /**
     * 获取评论详情
     */
    @Operation(summary = "获取评论详情", description = "获取指定评论的详细信息")
    @GetMapping("/{commentId}")
    public Result<CommentDTO> getCommentById(@Parameter(description = "评论ID") @PathVariable Long commentId) {
        CommentDTO comment = commentService.getCommentById(commentId);
        return Result.success("获取成功", comment);
    }
    
    /**
     * 获取帖子的评论列表
     */
    @Operation(summary = "获取帖子评论", description = "获取指定帖子的评论列表")
    @GetMapping("/post/{postId}")
    public Result<PageResult<CommentDTO>> getCommentsByPost(
            @Parameter(description = "帖子ID") @PathVariable Long postId,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") int page,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") int size) {
        PageResult<CommentDTO> comments = commentService.getCommentsByPost(postId, page, size);
        return Result.success("获取成功", comments);
    }
    
    /**
     * 获取评论的回复列表
     */
    @Operation(summary = "获取评论回复", description = "获取指定评论的回复列表")
    @GetMapping("/{commentId}/replies")
    public Result<PageResult<CommentDTO>> getRepliesByComment(
            @Parameter(description = "评论ID") @PathVariable Long commentId,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") int page,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") int size) {
        PageResult<CommentDTO> replies = commentService.getRepliesByComment(commentId, page, size);
        return Result.success("获取成功", replies);
    }
    
    /**
     * 点赞评论
     */
    @Operation(summary = "点赞评论", description = "为指定评论点赞")
    @PostMapping("/{commentId}/like")
    public Result<Void> likeComment(@Parameter(description = "评论ID") @PathVariable Long commentId) {
        commentService.likeComment(commentId);
        return Result.success("点赞成功");
    }
    
    /**
     * 取消点赞评论
     */
    @Operation(summary = "取消点赞", description = "取消对指定评论的点赞")
    @DeleteMapping("/{commentId}/like")
    public Result<Void> unlikeComment(@Parameter(description = "评论ID") @PathVariable Long commentId) {
        commentService.unlikeComment(commentId);
        return Result.success("取消点赞成功");
    }
} 